package api.controller;

import api.model.sif.SIFDoubleCardInfo;
import api.model.sifas.SIFASCardInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author xiakai
 * @create 2021-04-03 18:38
 */
@Controller
@RequestMapping("/sif")
public class SIFController {

    static Random random;

    static ArrayList<SIFDoubleCardInfo> doubleCardInfoList;

    @PostConstruct
    public void init() throws Exception {
        random = new Random();
        dealDoubleCardInfo();
    }

    @RequestMapping("/double-card")
    @ResponseBody
    public ModelAndView randomDoubleCard(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomDoubleCardInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomDoubleCardInfo());
            return mav;
        }
        return null;
    }

    private SIFDoubleCardInfo getRandomDoubleCardInfo() {
        return doubleCardInfoList.get(random.nextInt(doubleCardInfoList.size()));
    }

    private void dealDoubleCardInfo() throws IOException {
        // 背景
        doubleCardInfoList = new ArrayList<>();
        File file = new File("sif-double-card.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String address = br.readLine();
        String line = null;
        while ((line = br.readLine()) != null) {
            SIFDoubleCardInfo doubleCardInfo = new SIFDoubleCardInfo();
            String[] infoGroup = line.split("-");
            doubleCardInfo.setName(line);
            doubleCardInfo.setLeftId(infoGroup[1].substring(0, 4));
            doubleCardInfo.setRightId(infoGroup[1].substring(5));
            doubleCardInfo.setTeam(infoGroup[2]);
            doubleCardInfo.setLeftIdolName(infoGroup[3].substring(0, infoGroup[3].indexOf('_')));
            doubleCardInfo.setRightIdolName(infoGroup[3].substring(infoGroup[3].indexOf('_') + 1));
            doubleCardInfo.setType(infoGroup[4]);
            doubleCardInfo.setCollection(infoGroup[5]);
            doubleCardInfo.setRarity(infoGroup[6]);
            doubleCardInfo.setStatus(infoGroup[7]);
            doubleCardInfo.setUrl(address + URLEncoder.encode(line, "UTF-8").replaceAll("\\+", "%20"));
            doubleCardInfoList.add(doubleCardInfo);
        }
        br.close();
    }



}
